package com.system.manager.modules.risk.controller;


import com.system.manager.common.annotation.PreAuthorize;
import com.system.manager.common.entity.PageDTO;
import com.system.manager.common.entity.Result;
import com.system.manager.common.exception.SysException;
import com.system.manager.modules.authorized.entity.User;
import com.system.manager.modules.authorized.enums.SubjectType;
import com.system.manager.modules.risk.dto.FlowVo;
import com.system.manager.modules.risk.dto.RiskRiskVo;
import com.system.manager.modules.risk.dto.RiskVo;
import com.system.manager.modules.risk.dto.StaffStationVo;
import com.system.manager.modules.risk.entity.Risk;
import com.system.manager.modules.risk.service.IRiskService;
import com.system.manager.modules.risk.service.IStaffStationService;
import com.system.manager.modules.risk.service.ITbFlowService;
import com.system.manager.modules.risk.vo.UserDate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxp
 * @since 2021-11-16
 */
@Slf4j
@Api(tags = "风险模块")
@RestController
@RequestMapping("/risk/risk/")
public class RiskController {
    @Autowired
    private IRiskService iRiskService;
    //不用
    @ApiOperation("添加新的风险")
    @PreAuthorize({SubjectType.SYSTEM_ADMIN,SubjectType.RISK_ADMIN})
    @PostMapping("addRisk")
    public void addRisk(@RequestBody RiskVo riskVo){
        iRiskService.insertRisk(riskVo);
    }

    /**
     * 这个是最基本的查询所有的风险
     * @param page
     * @param size
     * @return
     */
    @ApiOperation("查看风险")
    @PreAuthorize({SubjectType.SYSTEM_ADMIN,SubjectType.RISK_ADMIN})
    @GetMapping("findAllRisk")
    public Result findRisk(@RequestParam(name = "page", required = false, defaultValue = "1")Integer page,
                           @RequestParam(name = "limit", required = false, defaultValue = "10")Integer size
                           ){
        UserDate<Risk> risk = iRiskService.findRisk(page, size);
        return Result.ok(risk.getData(), risk.getCount());
    }

    //添加新的风险
    @ApiOperation("添加新的风险")
    @PreAuthorize({SubjectType.SYSTEM_ADMIN,SubjectType.RISK_ADMIN})
    @PostMapping("addRiskRisk")
    public Result add(@RequestBody RiskRiskVo riskRiskVo){
        iRiskService.riskInsertRisk(riskRiskVo);
        return Result.ok();
    }


    /**
     * 根据条件查询出风险
     * @param page
     * @param size
     * @return
     */
    @ApiOperation("根据条件查询出风险")
    @PreAuthorize({SubjectType.SYSTEM_ADMIN,SubjectType.RISK_ADMIN})
    @GetMapping("findRisk")
    public PageDTO selectUsers(@RequestParam(name = "page", required = false, defaultValue = "1")Integer page,
                               @RequestParam(name = "limit", required = false, defaultValue = "10")Integer size,
                               @RequestParam(name = "fName", required = false)String fName,
                               @RequestParam(name = "rName",required = false)String rName,
                               @RequestParam(name = "rIssue",required = false)String rIssue){
        PageDTO<Risk> pageDTO = iRiskService.selectRiskCondition(page,size,fName,rName,rIssue);
        // 根据分页查询出所有的用户
        //PageDTO<User> pageDTO = userService.selectUsersCondition(page, size, role,userAccount, name, phone);

        return pageDTO;
    }

    /**
     * 删除用户
     */
    @PreAuthorize({SubjectType.SYSTEM_ADMIN,SubjectType.RISK_ADMIN})
    @ApiOperation("删除风险")
    @GetMapping("delete/{rId}")
    public Result delete(@PathVariable("rId")Integer rId){

        try {
            iRiskService.removeById(rId);

            return Result.ok();
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }

    }
}
